//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 08/05/2005
// Last Modif: 08/11/2005
//

var MOD_HGRP={};


function is_nuclindependent(cl) {
  return is_clindependent(cl);
}

function select_nuindep(list_in) {
  var list_out=new List();
  var iter= new List_iterator(list_in);  
  while(!iter.end()) {
    if( parseInt(iter.getValue()) && is_nuclindependent(iter.getValue())) {
      list_out.push(iter.getValue());
    }  
    iter.next();
  }    
  return list_out.getValue();
} 

function group_select(cl,callback) {
  if(!callback) callback="onchangegroup";
  var html="<select id='SG"+cl+"' onchange='"+callback+"("+cl+");'>";  
  var gr_iter= new group_iterator();
  html+="<option></option>";
  while(!gr_iter.end()) { 
    if(GetClaimGroup(cl)==gr_iter.getName()) {
      html+="<option selected>"+gr_iter.getName()+"</option>";
    } else {
      html+="<option>"+gr_iter.getName()+"</option>";
    }
    gr_iter.next();
  } 	 
  html+="</select>";
  return html;
}

function select_nudep(list_in) {
  var list_out=new List();
  var iter= new List_iterator(list_in);  
  while(!iter.end()) {
    if( parseInt(iter.getValue()) && !is_nuclindependent(iter.getValue())) {
      list_out.push(iter.getValue());
    }  
    iter.next();
  }    
  return list_out.getValue();
}

// return table with independent claim of dependent claims
MOD_HGRP.CL_CLOSURE;
MOD_HGRP.InitClosure=function() {
  MOD_HGRP.CL_CLOSURE=[];	  
  var cl_iter= new claim_iterator(); 
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    var dep=GetClaimDep(cl);
    if(dep_isindependent(dep)) {
      MOD_HGRP.CL_CLOSURE[cl]=cl;
    } else { 
      while(claim_isgrpdependent(dep)) { //!is_clindependent(dep)) {
	dep=GetClaimDep(dep);
      }
      MOD_HGRP.CL_CLOSURE[cl]=dep;
    }
    cl_iter.next();
  }
  //alert("Done");	
}
  

// check if dependent claims of a claim belong to same group as claim
function check_groups() {
  MOD_HGRP.InitClosure();
  var cl_iter= new claim_iterator(2); // skip claim 1
  var splitted=new List();  
  while(!cl_iter.end()) {
    var cl=cl_iter.getValue();
    if(claim_isgrpdependent(cl)) { // !is_clindependent(cl)) {
      var clos=MOD_HGRP.CL_CLOSURE[cl];
      if((clos!=1) && GetClaimGroup(cl) != GetClaimGroup(clos)) {
	splitted.push(clos);
      }
    }
    cl_iter.next();
  }
  return splitted.unique();
}



function onchangegroup(cl) {
  var idx=document.getElementById("SG"+cl).selectedIndex;
  SetClaimGroup(cl,GetGroupName(idx));
  var deps_on=claim_children(cl); // select_dep_on(cl);
  if(deps_on.length) {
    if(confirm("Propagate group "+GetGroupName(idx) +" to dependent claims ?")){
      var cl_iter= new claim_iterator(cl);
      while(!cl_iter.end()) {
	var gcl=cl_iter.getValue();
	SetClaimGroup(gcl,GetGroupName(idx));
	CheckClaimIntegrity(gcl);
        if(IsClaimInFilter(gcl)) {
	  document.getElementById("SG"+j).selectedIndex=idx;
	}
	cl_iter.next();
      }
    }
  }
}

function html_group_selection(id,pref,callback,opts) {
  return html_selection_iter(id,new groupname_iterator(),pref,callback,opts," ");
}

function html_group_doselect(selname,selval) {
  html_doselect_iter(selname,selval);
}


function Group2List(gname) {
  var list_out=new List();
  var cl_iter= new claim_iterator(1);
  while(!cl_iter.end()) {
    if(cl_iter.getGroup() == gname) {
      list_out.push(cl_iter.getValue());
    }
    cl_iter.next();
  }
  return list_out.getValue();
}

function GetGroupClaims(gname) {
  return List_tolist(Group2List(gname));
}

// find special technical features of claim cl against doc seldoc
function find_stfs_claim(seldoc,cl) { 
  return doc_nureferences(cl,seldoc,GetDossierLang(),"diff","raw");
}

// find claim with special technical features, if optcl use it.
function find_stfs(seldoc,gname,optcl) {
    var spclm;
    var stfs="";
    var stfscl="";
    //alert("find_stfs("+seldoc+","+gname+",)");
    if(optcl) {
      return "(Claim "+optcl+") "+find_stfs_claim(seldoc,optcl);
    }
    var cl_iter=new List_iterator(list_toList(GetGroupClaims(gname)));    
    while(!stfs  && !cl_iter.end()) {
      stfscl=cl_iter.getValue();
      stfs=find_stfs_claim(seldoc,stfscl);
      cl_iter.next();
    }
    if(stfs != "") {
      stfs= "(Claim "+stfscl+") "+separator_trim(stfs);
      SetGroupSTFClaim(seldoc,GetGroupIdx(gname),stfscl);
    }     	
    return stfs;
}

function grp_update_stfcl(seldoc,gname) {
  var cl_iter=new List_iterator(list_toList(GetGroupClaims(gname)));    
  var stfs="";
  var stfscl="";

  while(!stfs  && !cl_iter.end()) {
    stfscl=cl_iter.getValue();
    stfs=find_stfs_claim(seldoc,stfscl);
    cl_iter.next();
  }
  if(stfs != "") {
    SetGroupSTFClaim(seldoc,GetGroupIdx(gname),stfscl);
  }     	
  return stfscl;   
}


function find_stdstfs(seldoc,gname,optcl) {
  var spclm;
  var stfs="";
  var stfscl="";
  //alert("find_stfs("+seldoc+","+gname+",)");
  if(optcl) {
    return find_stfs_claim(seldoc,optcl);
  }
  var cl_iter=new List_iterator(list_toList(GetGroupClaims(gname)));    
  while(!stfs  && !cl_iter.end()) {
    stfscl=cl_iter.getValue();
    stfs=find_stfs_claim(seldoc,stfscl);
    cl_iter.next();
  }
  if(stfs != "") {
    stfs=separator_trim(stfs);
    SetGroupSTFClaim(seldoc,GetGroupIdx(gname),stfscl);
  }     	
  return stfs;
}


function find_prevstclaim(seldoc,gname) {
  var outlist=new List();
  var cl_iter=new List_iterator(list_toList(GetGroupClaims(gname)));
  var stcl=find_stclaim(seldoc,gname);
  while(!cl_iter.end()) {
    if(cl_iter.getValue()==stcl) {
      return outlist.getValue();
    }
    outlist.push(cl_iter.getValue());
    cl_iter.next();
  }
  return "";
}

// first claim with std feature
function find_stclaim(seldoc,gname) {
  return GetGroupPrefClaim(seldoc,GetGroupIdx(gname)) || GetGroupSTFClaim(seldoc,GetGroupIdx(gname)) || 0;
}

